package org.springblade.pm.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springblade.pm.service.IFileService;
import org.springblade.pm.vo.FileVo;
import org.springblade.pm.wrappper.FileWrapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping(value = "/file")
@AllArgsConstructor
public class FileController {
	private IFileService fileService;

	/**
	 * 保存文件
	 *
	 * @param file
	 * @return
	 */
	@PostMapping("/save")
	public R save(@RequestParam("file") MultipartFile file) {
		if (!file.isEmpty()) {
			String orgFilename = file.getOriginalFilename();
			org.springblade.pm.entity.File file1 = new org.springblade.pm.entity.File();
			//文件后缀名（文件类型）
			String fileName = orgFilename.substring(0, orgFilename.lastIndexOf("."));
			String suffix = orgFilename.substring(orgFilename.lastIndexOf("."));
			//文件类型判断逻辑
			String uuid = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
//		File dest = new File("d:/oa/invoiceImage" + uuid + suffix);
//		File saveDir = new File("d:/oa/invoiceImage");
			String osName = System.getProperty("os.name").toLowerCase();
			File dest = new File("/home/pm/file/" + uuid + suffix);
			File saveDir = new File("/home/pm/file/");
			if (osName.indexOf("windows") >= 0) {
				dest = new File("D:\\pm\\file\\" + uuid + suffix);
				saveDir = new File("D:\\pm\\file");
			}
			if (!saveDir.exists() && !saveDir.isDirectory()) {
				saveDir.mkdirs();
			}
			try {
				file.transferTo(dest);
			} catch (IOException e) {
				e.printStackTrace();
			}
			file.getContentType();
			file1.setOriginalName(fileName);
			file1.setName(uuid);
			file1.setType(suffix);
			return R.status(true);
		}
		return R.status(false);
	}

	/**
	 * 批量上传文件
	 *
	 * @param files
	 * @param file
	 * @return
	 */
	@PostMapping("/uploadFile")
	public R saveImages(@RequestParam("files") MultipartFile[] files, org.springblade.pm.entity.File file) {
		return R.status(fileService.saveBatch(files,file));
	}

	/**
	 * 批量删除文件
	 *
	 * @param ids
	 * @return
	 */
	@PostMapping("/remove")
	public R deleteImages(@RequestParam String ids) {
		return R.status(fileService.removeBatchByIds(ids));
	}

	/**
	 * 文件下载
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/downloadFile")
	public void downloadImage(HttpServletResponse response, @RequestParam String id) {
		fileService.downloadFile(response, id);
	}

	/**
	 * 文件预览
	 *
	 * @param response
	 * @param id
	 */
	@GetMapping(value = "preViewFile")
	public void preView(HttpServletResponse response, String id) {
		fileService.preView(response, id);
	}

	/**
	 * 批量下载
	 *
	 * @param ids
	 */
	@GetMapping(value = "/downloadBatch")
	public void downloadBatch(HttpServletResponse response, @RequestParam String ids, @RequestParam String zipName) {
		fileService.downloadBatch(response, ids,zipName);
	}

	/**
	 * 获取文件列表
	 *
	 * @param file
	 * @return
	 */
	@GetMapping(value = "/InvoiceFileList")
	public R<List<FileVo>> getInvoiceImageList(org.springblade.pm.entity.File file) {
		return R.data(FileWrapper.build().entityVOList(fileService.list(Wrappers.query(file))));
	}


}
